var numRun2;
var zhongjainginfo = null;
var isrunning = false;
var isrunningNum = 0;
var pnums;
$(function() {
	numRun2 = $(".numberRun2").numberAnimate({
		num : '100000000',
		speed : 1000,
		symbol : ","
	});
	localStorage.clear();
	 pnums = localStorage.getItem("persons");
	if(!pnums){
		pnums=1000;
	}else{
		pnums = parseInt(pnums);
	}
	$(".online").numberAnimate({
		num : ''+pnums,
		speed : 1000,
		symbol : ""
	});
	setInterval(function() {
		pnums = localStorage.getItem("persons");
		if(!pnums){
		pnums=1000;
	}else{
		pnums = parseInt(pnums);
	}
		pnums = parseInt(pnums);
		var m = pnums+RndNum(1);
		if(m>3000){
			 m = pnums-RndNum(1);
		}
		if(m<2000){
			m = pnums+RndNum(1);
		}
		localStorage.setItem("persons",""+m);
		$(".online").numberAnimate({
			num : ''+m,
			speed : 1000,
			symbol : ""
		});
	},10000);
	connect();
	showClassify();
	showSize();
	showSingle();
	showPrecise();
	// $('#timepop').css("visibility","visible");
	// alertSet(); //开始读秒
	$('#marquee').show();//滚动
	// $('#runalotterypop').css("visibility","visible");

});
function RndNum(n){
	  var rnd="";
	  for(var i=0;i<n;i++){
		  rnd+=Math.floor(Math.random()*10);
		  
	  }
	     
	  return parseInt(rnd);
	}
function setqiName(numRun2, qi, name) {
	$("#curr_qi2").text(name);
	isrunning = true;
	isrunningNum = 0;
	var n = 0;
	var i = setInterval(function() {
		var nums2 = getRand();
		numRun2.resetData(nums2);
		n++;
		isrunningNum=n;
		if (n > 4) {
			numRun2.resetData(qi);
			clearInterval(i);
			isrunning = false;
			setTimeout(function() {
				openStart();
			}, 1000);
		}
	}, 1000);
}
function openStart(){
	$.get(apihost + "openGame");
}
function kjl(){
	$.get(apihost + "kaijaing");
}
function getNowInfo() {
	
	$.get(apihost + "getResult",
					function(data) {
						if (isEmpty(data)) {

						} else {
							$(".curr_qiq").text(data.curr.name);
							var his = data.his;

							$("#hiss").empty();
							for (var i = 0; i < his.length; i++) {
								if (i == 0) {
									$("#curr_qi2").text(his[i].name);
									$("#curr_qi2_res").text(getresultText(his[i].result));
									var r = splitInts(his[i].result);
									if(r[1]==r[2]){
										$(".curr_qi2_res1").text(r[2]);
										$(".curr_qi2_res2").text(r[2]);
									}else{
										$(".curr_qi2_res1").text("-");
										$(".curr_qi2_res2").text(r[2]);
									}
									
									
									$(".numberRun2").numberAnimate({
										num : his[i].result,
										speed : 100,
										symbol : ","
									});
								} else {//
									var r = splitInts(his[i].result);
									var color = getresultColor(his[i].result);
									var html = '<div class="flex"><div class="flex-5 text-center flex"><div class="flex-2 text-center"><span class="inputSize whiteColor">'
											+ his[i].name
											+ '</span></div><div class="flex-1 text-center flex padding-right10"><div class="flex-1"><span class="inputSize whiteColor">'
											+ r[0]
											+ '</span></div><div class="flex-1"><span class="inputSize whiteColor">'
											+ r[1]
											+ '</span></div><div class="flex-1"><span class="inputSize" style="color:crimson;">'
											+ r[2]
											+ '</span></div></div></div><div class="flex-2 flex text-center"><div class="flex-1 flex h-center v-center"><span class="result '
											+ color[0]
											+ '">大</span></div><div class="flex-1 flex h-center v-center"><span class="result '
											+ color[1]
											+ '">小</span></div><div class="flex-1 flex h-center v-center"><span class="result '
											+ color[2]
											+ '">合</span></div></div></div>';
									$("#hiss").append(html);

								}
							}

							countDown(data.curr.times);

						}

					});
}
function splitInts(res) {
	var tmp1 = res.substr(6, 1);
	var tmp2 = res.substr(7, 1);
	var tmp3 = res.substr(8, 1);
	var data = new Array();
	data[0] = tmp1;
	data[1] = tmp2;
	data[2] = tmp3;
	return data;
}
function getresultText(res) {
	var tmp1 = res.substr(6, 1);
	var tmp2 = res.substr(7, 1);
	var tmp3 = res.substr(8, 1);
	if (tmp2 == tmp3) {
		return "合";
	} else {
		if (tmp3 > 4) {
			return "大";
		}
		if (tmp3 < 5) {
			return "小";
		}
	}

}

function getresultColor(res) {
	//
	var tmp1 = res.substr(6, 1);
	var tmp2 = res.substr(7, 1);
	var tmp3 = res.substr(8, 1);
	var data = new Array();
	data[0] = "";
	data[1] = "";
	data[2] = "";
	if (tmp2 == tmp3) {
		data[2] = "violetbackage";
	} else {
		if (tmp3 > 4) {
			data[0] = "violetbackage";
		}
		if (tmp3 < 5) {
			data[1] = "violetbackage";
		}
	}
	return data;

}
function getRand() {

	var str = Math.ceil(Math.random() * 9);
	for (var i = 0; i < 8; i++) {
		str += "" + Math.floor(Math.random() * (9 + 1));
	}
	return str;
}
// 倒计时的方法
function countDown(time) {
	var timeBoo = true;

	var i = setInterval(function() {
		if (timeBoo) {
			time -= 1;

			$('#countDown-unit').text(time - ((Math.floor(time / 10)) * 10));
			$('#countDown-decade').text(
					(time - ((Math.floor(time / 100)) * 100) - (time - ((Math
							.floor(time / 10)) * 10))) / 10);
			$("#js-sec-text").text(time);
			if(time<=10){
				$('#timepop').css("visibility","visible");
			}
			var t = time;
		    var n = document.getElementById("js-sec-circle");
		    var e = Math.round(t / 10 * 609);
		    	n.style.strokeDashoffset = e - 609;
	         $("#js-sec-text").text(time);
			if (time == 0) {
				$('#timepop').css("visibility","hidden");
				timeBoo = false;
				clearInterval(i);
				kjl();
			}
		}
	}, 1000);
}

// 分类场次的点击
function showClassify() {

	if (!isEmpty(getClassifyId($("#titleDiv div")))) {
		$('#sizeDiv').click(function() {
			if (getClassifyId($("#titleDiv div")) == "sizeDiv") {
				return false;
			}
			var ishas = $("#jc_middle").hasClass("fill-all");
			if(!ishas){
				$("#jc_middle").addClass("fill-all");
			}
			$("#game_type").val(1);
			deleteClassifyActive($("#titleDiv div"));
			$('#sizeDiv').addClass('active');
			// printLog("大小场");
			$('#sizeSession').show();
			$('#singleSession').hide();
			$('#preciseSession').hide();
			deleteClassifyActive($("#sizeButton div"));
			$('#size-small').addClass('active');
			$("#xia_type").val(1);
			$("#select_peilv").val($("#pelv_1").val());
			$("#yazhu_res").text("小");
		})
		$('#singleDiv').click(function() {
			if (getClassifyId($("#titleDiv div")) == "singleDiv") {
				return false;
			}
			var ishas = $("#jc_middle").hasClass("fill-all");
			if(!ishas){
				$("#jc_middle").addClass("fill-all");
			}
			deleteClassifyActive($("#titleDiv div"));

			$("#game_type").val(2);
			$('#singleDiv').addClass('active');
			// printLog("单双场");
			$('#sizeSession').hide();
			$('#singleSession').show();
			$('#preciseSession').hide();
			deleteClassifyActive($("#singleButton div"));
			$('#single-big').addClass('active');
			$("#xia_type").val(5);
			$("#select_peilv").val($("#pelv_2").val());
			$("#yazhu_res").text("大单");
		})
		$('#preciseDiv').click(function() {
			if (getClassifyId($("#titleDiv div")) == "preciseDiv") {
				return false;
			}

			var ishas = $("#jc_middle").hasClass("fill-all");
			if(ishas){
				$("#jc_middle").removeClass("fill-all");
			}
			$("#game_type").val(3);
			deleteClassifyActive($("#titleDiv div"));
			$('#preciseDiv').addClass('active');
			// printLog("精确场");
			$('#sizeSession').hide();
			$('#singleSession').hide();
			$('#preciseSession').show();
			deleteClassifyActive($("#preciseSession div .flex-1"));
			$('#precise-1').addClass('active');
			$("#xia_type").val(8);
			$("#xia_value").val(1);
			$("#yazhu_res").text("1");
			$("#select_peilv").val($("#pelv_3").val());
		})
	}
}
// 大小场里面的点击事件
function showSize() {

	$('#size-big').click(function() {

		deleteClassifyActive($("#sizeButton div"));
		$('#size-big').addClass('active');
		$("#xia_type").val(2);
		$("#select_peilv").val($("#pelv_1").val());
		$("#yazhu_res").text("大");
	})
	$('#size-small').click(function() {

		deleteClassifyActive($("#sizeButton div"));
		$('#size-small').addClass('active');
		$("#xia_type").val(1);
		$("#select_peilv").val($("#pelv_1").val());
		$("#yazhu_res").text("小");
	})
	$('#size-flat').click(function() {
		deleteClassifyActive($("#sizeButton div"));
		$('#size-flat').addClass('active');
		$("#xia_type").val(9);
		$("#select_peilv").val($("#pelv_9").val());
		$("#yazhu_res").text("合");
	})

}
// 大小单双场里面的点击事件
function showSingle() {
	$('#single-big').click(function() {

		deleteClassifyActive($("#singleButton div"));
		$('#single-big').addClass('active');
		$("#xia_type").val(5);
		$("#select_peilv").val($("#pelv_2").val());
		$("#yazhu_res").text("大单");
	})
	$('#single-small').click(function() {

		deleteClassifyActive($("#singleButton div"));
		$('#single-small').addClass('active');
		$("#xia_type").val(4);
		$("#select_peilv").val($("#pelv_2").val());
		$("#yazhu_res").text("小单");
	})
	$('#double-big').click(function() {
		deleteClassifyActive($("#singleButton div"));
		$('#double-big').addClass('active');
		$("#xia_type").val(6);
		$("#select_peilv").val($("#pelv_2").val());
		$("#yazhu_res").text("大双");
	})
	$('#double-small').click(function() {
		deleteClassifyActive($("#singleButton div"));
		$('#double-small').addClass('active');
		$("#xia_type").val(3);
		$("#yazhu_res").text("0");
		$("#select_peilv").val($("#pelv_2").val());
		$("#yazhu_res").text("小双");
	})
}
// 精确场里面的点击事件
function showPrecise() {
	$('#precise-0').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-0').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(0);
		$("#yazhu_res").text("0");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-1').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-1').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(1);
		$("#yazhu_res").text("1");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-2').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-2').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(2);
		$("#yazhu_res").text("2");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-3').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-3').addClass('active');
		$("#xia_value").val(3);
		$("#xia_type").val(8);
		$("#yazhu_res").text("3");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-4').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-4').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(4);
		$("#yazhu_res").text("4");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-5').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-5').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(5);
		$("#yazhu_res").text("5");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-6').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-6').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(6);
		$("#yazhu_res").text("6");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-7').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-7').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(7);
		$("#yazhu_res").text("7");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-8').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-8').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(8);
		$("#yazhu_res").text("8");
		$("#select_peilv").val($("#pelv_3").val());
	})
	$('#precise-9').click(function() {
		deleteClassifyActive($("#preciseSession div .flex-1"));
		$('#precise-9').addClass('active');
		$("#xia_type").val(8);
		$("#xia_value").val(9);
		$("#yazhu_res").text("9");
		$("#select_peilv").val($("#pelv_3").val());
	})
}
// 增加钱的按钮
function addMonet(inputID) {
	var m = $('.moneyInput').val();
	var max = parseInt($('#xia_max').val());
	m = parseInt(m) + 1;
	if (m > max) {
		showSuccess("最大注为" + max);
	} else {
		$('.moneyInput').val(m);
	}
}
// 减少钱
function reduceMonet(inputID) {
	var m = $('.moneyInput').val();
	var min = parseInt($('#xia_min').val());
	m = parseInt(m) - 1;
	if (m < min) {
		showSuccess("最小注为" + min);
	} else {
		$('.moneyInput').val(m);
	}
}

function btnMax() {
	var max = parseInt($('#xia_max').val());
	$('.moneyInput').val(max);
}

function deleteClassifyActive(temp) {
	temp.each(function() {
		var aa = $(this);
		if (aa.hasClass('active')) {
			aa.removeClass('active');
		}
	});
}
// 获取被选择的分类id
function getClassifyId(temp) {
	var classifyId = "";
	temp.each(function() {
		var aa = $(this);
		if (aa.hasClass('active')) {
			classifyId = aa.attr("id");
		}
	});
	return classifyId;
}
// 往期回顾 就是记录
function lookBack() {
	// printLog("打开记录");
	window.location.href = "record.html";
}
// 关闭说明
function popboxclose() {
	$('#explainpop').css("visibility", "hidden");
}
// 玩法说明
function explain() {
	$('#explainpop').css("visibility", "visible");
}

function connect() {
	ws = new WebSocket("ws://"+pushhost);
	ws.onerror = function() {
		connect();
	};
	ws.onclose = function() {
		connect();
	};
	ws.onmessage = function(e) {
		// json数据转换成js对象
		var data = eval("(" + e.data + ")");
		var type = data.type || '';
		switch (type) {
		case "ping":
			ws.send('{"type":"pong"}');
			break;
		case "refresh":
			var ttt = (6-isrunningNum)*1000;
			setTimeout(function(){
				getNowInfo();
			},ttt);
			break;
		case "result":			
			$('#timepop').css("visibility","hidden");
			setqiName(numRun2, data.data.res, data.data.name);
			break;
		case "zjall":
			setTimeout(function(){
			   	
			   	var scrollData =data.data;
			   	$("#marquee").empty();
			   
			   	var m1 = $('<marquee id="marquee1" direction="left" behavior="scroll" scrollamount="5" scrolldelay="0" loop="1" width="100%" height="100%"  vspace="2"></marquee>');
			   	var m2 = $('<marquee style="margin-left:20px" id="marquee2" direction="left" behavior="scroll" scrollamount="5" scrolldelay="0" loop="1" width="100%" height="100%"  vspace="2"></marquee>');
			   	$("#marquee").append(m1);
			   	$("#marquee").append(m2);
			   	for(var i=0;i<scrollData.length;i++){
			   		var t = '<font class="marqueefont">恭喜<span style="color:blue;">'+scrollData[i].name+'</span>中奖'+scrollData[i].money+'</font>';
			   		if (i%2==0) {
			   		$("#marquee1").append(t);
			   		} else{
			   			$("#marquee2").append(t);
			   		}
			   	}
			   	
			   },6000);
			
			break;
		case "zhongjaing":
			zhongjainginfo = data.data;
			$("#kaijiangjieguo").text("恭喜你");
			setTimeout(function(){
				getMymoney();
				zhongjaing();
			},4000);	
			break;
		case "shule":
			zhongjainginfo = data.data;
			$("#kaijiangjieguo").text("很遗憾");
			setTimeout(function(){
				getMymoney();
				zhongjaing();
			},4000);	
			break;
		case "countChange":
			/*$(".online").numberAnimate({
				num : ''+data.data,
				speed : 1000,
				symbol : ""
			});*/
			break;
		// Events.php中返回的init类型的消息，将client_id发给后台进行uid绑定
		case 'init':
			// 利用jquery发起ajax请求，将client_id发给后端进行uid绑定
			$.post(apihost+'bind', {
				client_id : data.client_id
			}, function(res) {
				openStart();
				getNowInfo();
			}, 'json');
			break;
		}
	};
}
// 确认下注弹窗确认按钮
function betonpopok() {
	$('#sizebetonpop').css("visibility", "hidden");
	var obj = {type:$("#game_type").val(),xiazhutype:$("#xia_type").val(),money:$('.moneyInput').val(),num:$("#xia_value").val()};
	post(apihost+"xiazhu",obj,function(data){
		if(data.flag==0){
			$(".myyue").text(data.data);
			showSuccess("下注成功");
		}else{
			showSuccess(data.msg);
		}
		
	});
	
	
	
}

function zhongjaing() {
	if (zhongjainginfo != null) {
		$("#runalotterypop").css("visibility","visible");
		$("#res_qi_name").text(zhongjainginfo.name+"期");
		$("#result_num_1").text(zhongjainginfo.num[2]);
		$("#result_num_2").text(zhongjainginfo.num[1]);
		$("#result_num_3").text(zhongjainginfo.num[0]);
		var data = zhongjainginfo.result;
		$("#xia_res_container").empty();
		
		for(var i=0;i<data.length;i++){
			var tmp = data[i];
			var typename = "";
			
			if(tmp['type']==1){
				typename="大小";
			}else if(tmp['type']==2){
				typename="大小单双";
			}else if(tmp['type']==3){
				typename="精确";
			}
			
			var zhuanmoney = 0;
			if(tmp['is_ying']==1){
				zhuanmoney = tmp['ying_money'];
			}
			
			
			var html = '<div class="flex text-center title-div"style="height: 25px;border-bottom: 1px solid floralwhite;"><div class="flex-3" >'+
			'<span>'+typename+'</span></div><div class="flex-2 flex h-center"><span id="jc_xia_name">'+tmp['jc_name']+'</span></div><div class="flex-2 flex h-center">'+
			'<span id="jc_xia_money">'+(tmp['money']/100)+'</span></div><div class="flex-2 flex h-center"><span id="jc_res_money">'+zhuanmoney+'</span></div></div>';
			$("#xia_res_container").append(html);
		}
		
		
		zhongjainginfo = null;
	}
}
// 关闭确认下注弹窗
function betonpopclose(temp) {
	$('#sizebetonpop').css("visibility", "hidden");
}
// 下注按钮
function bottompoursize(temp) {
	var m = $('.moneyInput').val();
	var min = parseInt($('#xia_min').val());
	if (m < min) {
		showSuccess("最小注为" + min);
	} else {
		changexiazhu();
		$('#sizebetonpop').css("visibility", "visible");
	}

}

function changexiazhu() {
	$("#yazhu_peilv").text($("#select_peilv").val());
	var m = parseInt($('.moneyInput').val());
	var peilv = parseFloat($("#select_peilv").val());
	$("#yazhu_money").text(m);
	$("#yazhu_getMoney").text(parseFloats(m * peilv));
}
